package com.shixun.blog.controller;


import com.shixun.blog.domain.BlogArticle;
import com.shixun.blog.domain.BlogUser;
import com.shixun.blog.service.IBlogArticleService;
import com.shixun.blog.service.IBlogUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("blog")
public class BlogController {

    @Autowired
    private IBlogArticleService blogArticleService;

    @Autowired
    private IBlogUserService blogUserService;

    @RequestMapping("index")
    public String index(ModelMap map) {

        List<BlogArticle> blogArticles = blogArticleService.selectBlogArticleList(null);
        map.put("blogArticles",blogArticles);

        List<BlogUser> blogUsers = blogUserService.selectBlogUserList(null);


        map.put("blogUser",blogUsers.get(0));
        return "/blog/index";
    }


    @RequestMapping("userblog/{id}")
    public String userblog(@PathVariable("id") Long userId,ModelMap map){

        BlogArticle blogArticle = new BlogArticle();
        blogArticle.setAuthor(String.valueOf(userId));
        BlogUser blogUser = blogUserService.selectBlogUserById(userId);
        List<BlogArticle> blogArticles = blogArticleService.selectBlogArticleList(blogArticle);

        map.put("blogArticles",blogArticles);
        map.put("blogUser",blogUser);

        return "/blog/index";
    }
}
